package com.jd.service.impl;

import com.alibaba.fastjson.JSON;
import com.jd.common.E3Result;
import com.jd.dao.AdminMapper;
import com.jd.pojo.Admin;
import com.jd.pojo.AdminExample;
import com.jd.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService{


    @Autowired
    private AdminMapper adminMapper;

    /**
     * 验证管理员密码是否正确
     * @param admin
     * @param session
     * @return
     */
    @Override
    public E3Result checkUserNameAndPasswd(Admin admin , HttpSession session) {

        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        List<Admin> admins = adminMapper.selectByExample(example);
        System.out.println("查出来的数据： " + JSON.toJSONString(admins));

        boolean temp = false;
        for(Admin ad : admins){
            if(ad.getAdminname().equals(admin.getAdminname()) && ad.getAdminpassword().equals(admin.getAdminpassword())){
                temp = true;
                break;
            }
        }
        if(temp){
            //登录成功
            session.setAttribute("user",admin);
            return E3Result.ok(admin);
        }else{
            return E3Result.build(400,"账号或者密码错误");
        }

    }
}
